<template>
  <div class="checkout-address">
    <div class="text">
      <!-- <div class="none">您需要先添加收货地址才可提交订单。</div> -->
      <ul v-if="showAddress">
        <li><span>收<i/>货<i/>人：</span>{{ showAddress.receiver }}</li>
        <li><span>联系方式：</span>{{ showAddress.contact.replace(/(\d{3})\d{4}(\d{4})$/,'$1****$2') }}</li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation.replace(/ /g,'') }} {{ showAddress.address }}</li>
      </ul>
      <a href="javascript:;" @click="openAddressEdit(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="openDialog">切换地址</XtxButton>
      <XtxButton class="btn" @click="confirmAddress({})">添加地址</XtxButton>
    </div>
  </div>
  <XtxDiaLog title="切换收货地址" v-model:visible="dialogVisible">
   <template #main>
    <div class="text item" v-for="item in list" :key="item.id"
    :class="{active: selectedAddress&&item.id === selectedAddress.id}"
    @click="selectedAddress = item"
    >
      <ul>
        <li><span>收<i/>货<i/>人</span>{{ item.receiver }}</li>
        <li><span>联系方式</span>{{item.contact}}</li>
        <li><span>收货地址</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
      </ul>
    </div>
   </template>
   <template #footer>
    <XtxButton type="gray" style="margin-right:20px" @click="dialogVisible=false">取消</XtxButton>
    <XtxButton type="primary" @click="selectReceipt">确认</XtxButton>
   </template>
  </XtxDiaLog >
  <EditAddress ref="editAddress" @on-success="successHandler"/>
</template>
<script>
import { ref } from 'vue'
import XtxDiaLog from '@/components/library/xtx-dialog.vue'
import EditAddress from './address-edit.vue'
export default {
  name: 'CheckoutAddress',
  components: {
    XtxDiaLog,
    EditAddress
  },
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  emits: ['change'],
  setup (props, { emit }) {
    const showAddress = ref(null)
    console.log(props.list.length)

    // 判断是否有收货地址
    if (props.list.length) {
      // 查询默认的地址
      const defaultAddress = props.list.find(item => item.isDefault === 1)
      // 判断是否是默认地址
      if (defaultAddress) {
        showAddress.value = defaultAddress
      } else {
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAddress.value = props.list[0]
      }
    }

    // 默认通知父组件一个收货地址
    emit('change', showAddress.value?.id)
    // 切换收货地址弹窗
    const dialogVisible = ref(false)
    // 选择地址
    const selectedAddress = ref(null)

    const openDialog = () => {
      dialogVisible.value = true
      selectedAddress.value = null
      emit('change', showAddress.value?.id)
    }

    const editAddress = ref(null)
    // 添加收货地址
    const confirmAddress = (address) => {
      editAddress.value.open(address)
    }
    const successHandler = (fromData) => {
      const address = props.list.find(item => item.id === fromData.id)
      if (address) {
        for (const key in address) {
          address[key] = fromData[key]
        }
      } else {
        // 如果是添加 就往list中追加一条
        // 当再修改formData的时候，数据中的数据也会更新，因为是同一引用的地址
        // 当打开对话框的时候需要情况之前的输入信息
        // 克隆formData的数据
        const jsonStr = JSON.stringify(fromData)

        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(JSON.parse(jsonStr))
      }
    }
    // 打开修改地址编辑地址
    const openAddressEdit = (address) => {
      editAddress.value.open(address)
    }

    // 选择收货地址
    const selectReceipt = () => {
      dialogVisible.value = false
      showAddress.value = selectedAddress.value
      emit('change', selectedAddress.value.id)
    }
    return { showAddress, dialogVisible, openDialog, selectedAddress, confirmAddress, editAddress, successHandler, openAddressEdit, selectReceipt }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}

</style>
